/*
 * Copyright (c) 2008 Sergey Rudchenko <sergey.rudchenko@gmail.com>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "image_pool.h"
#include <SDL_image.h>
#include <string.h>

#define IMAGE_COUNT 4

static SDL_Surface* image_pool[IMAGE_COUNT];

#define IMAGE_DIR INSTALL_PREFIX "/share/relaxian/"

static int load_image(int id, char* name)
{
    char* filename = malloc(strlen(IMAGE_DIR) + strlen(name) + 5 /* .ext\0 */);
    strcpy(filename, IMAGE_DIR);
    strcat(filename, name);
    strcat(filename, ".png");

    image_pool[id] = IMG_Load(filename);

    if (!image_pool[id])
        printf("Error: %s\n", SDL_GetError());

    free(filename);
    return image_pool[id] == NULL; // non-zero on error
}

int load_images()
{
    return load_image(IMAGE_SHIP,    "ship")    ||
           load_image(IMAGE_MISSILE, "missile") ||
           load_image(IMAGE_SOLDIER, "soldier") ||
           load_image(IMAGE_STAR,    "star");
}

void unload_images()
{
    int i;
    for (i = 0; i < IMAGE_COUNT; ++i)
    {
        SDL_FreeSurface(image_pool[i]);
        image_pool[i] = NULL; /* safety match */
    }
}

SDL_Surface* get_image(int id)
{
    return image_pool[id];
}

